package com.shenmazong.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.library.entity.TbExamChapter;
import com.shenmazong.library.entity.TbExamSkillDetail;
import com.shenmazong.library.entity.TbExamSkillLibrary;
import com.shenmazong.library.entity.TbExamTheoryLibrary;
import com.shenmazong.library.mapper.TbExamChapterMapper;
import com.shenmazong.library.mapper.TbExamSkillDetailMapper;
import com.shenmazong.library.mapper.TbExamSkillLibraryMapper;
import com.shenmazong.library.service.ExamLibraryService;
import com.shenmazong.library.mapper.TbExamTheoryLibraryMapper;
import com.shenmazong.util.ChooseIsNull;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ExamCheatVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *serviceImpl Qi
 */
@Service
public class TbExamTheoryLibraryServiceImpl extends ServiceImpl<TbExamTheoryLibraryMapper, TbExamTheoryLibrary>
    implements ExamLibraryService{

    @Autowired
    private TbExamChapterMapper chapterMapper;
    @Autowired
    private TbExamSkillDetailMapper detailMapper;
    @Autowired
    private TbExamSkillLibraryMapper skillMapper;
    @Autowired
    private  TbExamTheoryLibraryMapper  theoryMapper;


    @Override
    public ResultResponse saveExamChapter(TbExamChapter tbExamChapter) {
        int i = chapterMapper.insert(tbExamChapter);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(chapterMapper.selectById(tbExamChapter.getChapterId()));
    }

    @Override
    public ResultResponse deleteExamChapter(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = chapterMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamChapter(TbExamChapter tbExamChapter) {
        int i = chapterMapper.updateById(tbExamChapter);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(chapterMapper.selectById(tbExamChapter.getChapterId()));
    }

    @Override
    public ResultResponse listExamChapter(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        QueryWrapper<TbExamChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamChapter::getSchoolId,paramVo.getParamMap().get("schoolId"));

        IPage<TbExamChapter> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamChapter> selectPage = chapterMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse listAllExamChapter(TbExamChapter tbExamChapter) {
        return ResultResponse.SUCCESS(chapterMapper.selectList(new QueryWrapper<TbExamChapter>().eq("school_id",tbExamChapter.getSchoolId())));
    }

    @Override
    public ResultResponse saveExamSkillDetail(TbExamSkillDetail tbExamSkillDetail) {
        int i = detailMapper.insert(tbExamSkillDetail);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(detailMapper.selectById(tbExamSkillDetail.getSkillDetailId()));
    }

    @Override
    public ResultResponse deleteExamSkillDetail(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = detailMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamSkillDetail(TbExamSkillDetail tbExamSkillDetail) {
        int i = detailMapper.updateById(tbExamSkillDetail);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(detailMapper.selectById(tbExamSkillDetail.getSkillDetailId()));
    }

    @Override
    public ResultResponse listExamSkillDetail(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("skillId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }
        QueryWrapper<TbExamSkillDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamSkillDetail::getSkillId,paramVo.getParamMap().get("skillId"));

        IPage<TbExamSkillDetail> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamSkillDetail> selectPage = detailMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse saveExamSkillLibrary(TbExamSkillLibrary tbExamSkillLibrary) {
        int i = skillMapper.insert(tbExamSkillLibrary);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(skillMapper.selectById(tbExamSkillLibrary.getSkillId()));
    }

    @Override
    public ResultResponse deleteExamSkillLibrary(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = skillMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamSkillLibrary(TbExamSkillLibrary tbExamSkillLibrary) {
        int i = skillMapper.updateById(tbExamSkillLibrary);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(skillMapper.selectById(tbExamSkillLibrary.getSkillId()));
    }

    @Override
    public ResultResponse listExamSkillLibrary(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }
        QueryWrapper<TbExamSkillLibrary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamSkillLibrary::getSchoolId,paramVo.getParamMap().get("schoolId"));

        IPage<TbExamSkillLibrary> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamSkillLibrary> selectPage = skillMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse saveExamTheoryLibrary(TbExamTheoryLibrary tbExamTheoryLibrary) {
        int i = theoryMapper.insert(tbExamTheoryLibrary);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(theoryMapper.selectById(tbExamTheoryLibrary.getTheoryId()));
    }

    @Override
    public ResultResponse deleteExamTheoryLibrary(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = theoryMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamTheoryLibrary(TbExamTheoryLibrary tbExamTheoryLibrary) {
        int i = theoryMapper.updateById(tbExamTheoryLibrary);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(theoryMapper.selectById(tbExamTheoryLibrary.getTheoryId()));
    }

    @Override
    public ResultResponse listExamTheoryLibrary(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }
        QueryWrapper<TbExamTheoryLibrary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamTheoryLibrary::getSchoolId,paramVo.getParamMap().get("schoolId"));

        IPage<TbExamTheoryLibrary> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamTheoryLibrary> selectPage = theoryMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }
}




